package ph.com.gs3.formalistics.global.utilities;

import java.util.ArrayList;
import java.util.List;

public class DependencyChecker {

	public static void checkDependencies(String[] dependencyClasses, Object[] dependencies) {

		List<String> unsatisfiedDependencies = new ArrayList<String>();
		
		for (int i = 0; i < dependencyClasses.length; i ++) {
			if (dependencies[i] == null) {
				unsatisfiedDependencies.add(dependencyClasses[i]);
			}
		}

		if (unsatisfiedDependencies.size() > 0) {
			throw new UnsatisfiedDependencyException(unsatisfiedDependencies);
		}

	}

	public static class UnsatisfiedDependencyException extends RuntimeException {

		private static final long serialVersionUID = 7867607849168809583L;

		private String message;

		public UnsatisfiedDependencyException(List<String> unsatisfiedDependencies) {
			String message = "The following dependencies are unsatisfied: ";
			for (String dependency : unsatisfiedDependencies) {
				message += dependency + ", ";
			}

			// remove the trailing ", "
			message = message.substring(0, message.length() - 2);
			this.message = message;
		}

		@Override
		public String getMessage() {
			return message;
		}

	}

}
